package org.jash.learnmvp.chat;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;


import org.jash.learnmvp.R;

import java.util.ArrayList;
import java.util.List;

class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder> {
   private List<EMMessage> data = new ArrayList<>();
   @NonNull
   @Override
   public MessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
      return new MessageViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message, parent, false));
   }

   @Override
   public void onBindViewHolder(@NonNull MessageViewHolder holder, int position) {
      EMMessage message = data.get(position);
      holder.body.setText(((EMTextMessageBody) message.getBody()).getMessage());
      Glide.with(holder.icon)
              .load("http://gips2.baidu.com/it/u=195724436,3554684702&fm=3028&app=3028&f=JPEG&fmt=auto?w=1280&h=960")
              .transform(new CircleCrop())
              .into(holder.icon);

   }

   @Override
   public int getItemCount() {
      return data.size();
   }
   public void addAll(List<EMMessage> list) {
      int size = data.size();
      data.addAll(list);
      notifyItemRangeInserted(size, list.size());
   }
   public void addAll(int index, List<EMMessage> list) {
      data.addAll(index, list);
      notifyItemRangeInserted(index, list.size());
   }

   public static class MessageViewHolder extends RecyclerView.ViewHolder {
      private ImageView icon;
      private TextView body;
      public MessageViewHolder(@NonNull View itemView) {
         super(itemView);
         icon = itemView.findViewById(R.id.icon);
         body = itemView.findViewById(R.id.body);
      }
   }
}
